/**
 * 更新数据
 */
function ups(id ,msg , k1,v1,url='save'){ //save是路径，state是状态字段
    if(msg && !confirm(msg)){
        return
    }
    let data = {};
    data.id = id;
    data[k1] = v1
    $.ajax({
        type: "POST",
        url,
        data,
        success: function (data) {
            alert(data.message);
            if(data.success){
                location.href="list";//刷新
            }
        },
        error: function () {
            alert("请求服务器失败，请检查Java控制台报错");
        }
    });
}
/**
 * 删除功能
 * @param id
 */
function del(id,msg='删除') {
    if(confirm(`确定${msg}`)){
        $.ajax({
            type: "GET",
            url: "delete?id="+id,
            success: function (data) {
                alert(data.message);
                if(data.success){
                    location.href="list";
                }
            },
            error: function () {
                alert("请求服务器失败，请检查Java控制台报错");
            }
        });
    }
}
//提交表单
function gotoPage(index) {
    $("#pageIndex").val(index);
    $("#dataForm").submit();
}

//del batch
// $(function () {
//     $(".t-check-h").change(function () {
//         if(!$(this).is(":checked")){//未选中
//             $(".t-check").prop("checked", false);
//         }else {//选中
//             $(".t-check").prop("checked", true);
//         }
//     })
// })
// function delAll() {
//     let list = []
//     $(".t-check:checked").each(function (index,element) {
//         list.push(element.value)
//     })
//     if(list.length>0){
//         console.log(list)
//         del(list.join())
//     }
//     else alert("未选择任何行")
// }

//模态窗口
var modalBox = (function() {
    var modalBox = {};
    modalBox.modal = document.getElementById("myModal");
    modalBox.closeBtn = document.getElementById("closeBtn");
    modalBox.show = function() {
        console.log(this.modal);
        this.modal.style.display = "block";
    }
    modalBox.close = function() {
        this.modal.style.display = "none";
    }
    modalBox.outsideClick = function() {
        var modal = this.modal;
        window.onclick = function(event) {
            if(event.target == modal) {
                modal.style.display = "none";
            }
        }
    }
    modalBox.init = function() {
        var that = this;
        if(this.closeBtn){
            this.closeBtn.onclick = function() {
                that.close();
            }
        }
        this.outsideClick();
    }
    modalBox.init();
    return modalBox;
})();
//import excel
$("#submitExcel").click(function () {
    var formData = new FormData();
    formData.append("excel",$("#fileExcel")[0].files[0]);
    $.ajax({
        url : "excel?action=import",
        type : "POST",
        data : formData,
        processData : false,
        contentType : false,
        success : function(res) {
            alert(res.message)
            if(res.success){
                modalBox.close()
                gotoPage(1)
            }
        }
    });
})
